package com.stylefeng.guns.modular.warehouse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.InitWarehouseID;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.WarehouseMapper;
import com.stylefeng.guns.modular.system.model.Route;
import com.stylefeng.guns.modular.system.model.Warehouse;
import com.stylefeng.guns.modular.warehouse.service.IWarehouseService;

/**
 * 仓库信息控制器
 *
 * @author fengshuonan
 * @Date 2018-04-01 16:12:20
 */
@Controller
@RequestMapping("/warehouse")
public class WarehouseController extends BaseController {

    private String PREFIX = "/warehouse/warehouse/";

    @Autowired
    private IWarehouseService warehouseService;
    @Autowired
    private WarehouseMapper warehouseMapper;

    /**
     * 跳转到仓库信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "warehouse.html";
    }

    /**
     * 跳转到添加仓库信息
     */
    @RequestMapping("/warehouse_add")
    public String warehouseAdd() {
        return PREFIX + "warehouse_add.html";
    }

    /**
     * 跳转到修改仓库信息
     */
    @RequestMapping("/warehouse_update/{warehouseId}")
    public String warehouseUpdate(@PathVariable Integer warehouseId, Model model) {
        Warehouse warehouse = warehouseService.selectById(warehouseId);
        model.addAttribute("item",warehouse);
        LogObjectHolder.me().set(warehouse);
        return PREFIX + "warehouse_edit.html";
    }

    /**
     * 获取仓库信息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<Warehouse> page = new PageFactory<Warehouse>().defaultPage();
//    	Page<Consignee> page = new PageFactory<Consignee>().defaultPage();
    	if(ToolUtil.isEmpty(condition)){
    		page.setRecords(warehouseMapper.selectPage(page, null));
    		return super.packForBT(page);
    	}else{
    		EntityWrapper<Warehouse> entityWrapper = new EntityWrapper<>();
    		Wrapper<Warehouse> wrapper = entityWrapper.like("warehouse_name", condition);
    		page.setRecords(warehouseMapper.selectPage(page,wrapper));
    		return super.packForBT(page); 
    	}
    }

    /**
     * 新增仓库信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Warehouse warehouse) {
    	warehouse.setWarehouseId(InitWarehouseID.getInstance().initDisorderID());
        warehouseService.insert(warehouse);
        return SUCCESS_TIP;
    }

    /**
     * 删除仓库信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer warehouseId) {
        warehouseService.deleteById(warehouseId);
        return SUCCESS_TIP;
    }

    /**
     * 修改仓库信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Warehouse warehouse) {
        warehouseService.updateById(warehouse);
        return SUCCESS_TIP;
    }

    /**
     * 仓库信息详情
     */
    @RequestMapping(value = "/detail/{warehouseId}")
    @ResponseBody
    public Object detail(@PathVariable("warehouseId") Integer warehouseId) {
        return warehouseService.selectById(warehouseId);
    }
}
